﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using ShopB2C.Helper;
using ShopB2C.Interfaces;

namespace ShopB2C.Controllers.Base {
    public partial class BaseController : Controller {
        public BaseController() {
            // 站点地图
            if (ViewData["SiteMapPath"] != null) {
                ((List<string>)ViewData["SiteMapPath"]).Clear();
            }
            else {
                ViewData["SiteMapPath"] = new List<string>();
            }
        }
        /// <summary>
        /// 添加 SiteMapPath 路径
        /// </summary>
        /// <param name="url">路径</param>
        protected void SiteMapPathAdd(string linkText) {
            this.SiteMapPathAdd(linkText, null);
        }
        /// <summary>
        /// 添加 SiteMapPath 路径
        /// </summary>
        /// <param name="linkText">链接文字</param>
        /// <param name="url">路径</param>
        protected void SiteMapPathAdd(string linkText, string url) {
            List<string> urls = (List<string>)ViewData["SiteMapPath"];
            urls.Add(string.IsNullOrEmpty(url)
                ? linkText
                : string.Format("<a href=\"{0}\" target=\"_self\">{1}</a>", url, linkText));
            ViewData["SiteMapPath"] = urls;
        }
        /// <summary>
        /// 修改 SiteMapPath 路径
        /// </summary>
        /// <param name="oldLinkText">原链接文字</param>
        /// <param name="newLinkText">新链接文字</param>
        protected void SiteMapPathEdit(string oldLinkText, string newLinkText) {
            this.SiteMapPathEdit(oldLinkText, newLinkText, null);
        }
        /// <summary>
        /// 修改 SiteMapPath 路径
        /// </summary>
        /// <param name="oldLinkText">原链接文字</param>
        /// <param name="newLinkText">新链接文字</param>
        /// <param name="url">链接路径</param>
        protected void SiteMapPathEdit(string oldLinkText, string newLinkText, string url) {
            List<string> urls = (List<string>)ViewData["SiteMapPath"];
            int index = urls.FindIndex(s => { return s.Contains(oldLinkText); });
            if (index >= 0 && index < urls.Count) {
                urls[index] = string.IsNullOrEmpty(url)
                    ? newLinkText
                    : string.Format("<a href=\"{0}\" target=\"_self\">{1}</a>", url, newLinkText);
            }
            ViewData["SiteMapPath"] = urls;
        }

        /// <summary>
        /// 创建分页匿名对象
        /// </summary>
        /// <param name="HrefFormat">Url 格式字符串，
        /// 注意 UrlHelper 会将 {0} 转义，链接到字符串需要占用格式化字符串 {0} 占位符</param>
        /// <param name="pageSize">总页数</param>
        /// <param name="pageIndex">当前页码</param>
        /// <returns>分页匿名对象</returns>
        protected virtual object CreatePager(string HrefFormat, int pageCount, int pageIndex) {
            return new {
                PageIndex = pageIndex,
                PageCount = pageCount,
                HrefFormat = Server.UrlDecode(HrefFormat)
            };
        }
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="to">发送到联系人</param>
        /// <param name="subject">邮件主题</param>
        /// <param name="body">邮件正文</param>
        protected void SendMail(string to, string subject, string body) {
            MailHelper mailHelper = new MailHelper(_smtpHost, _smtpPort, _smtpUser, _smtpPassword);
            mailHelper.SendAsyncMail(_smtpFrom, to, subject, body);
        }
    }
}
